//Mon 22 Feb 2010 12:19:29 AM CST 
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define		ITER	1000
class AgeEncoding
{
public:
	double getRadix(int age, string candles)
	{
		while (candles.length () > 0 && candles [0] == '0')
            candles = candles.substr (1);
 
        if (candles.length () == 0)
            return -1;
 
        if (candles.length () == 1)
            return age == 1 ? -2 : -1;
 
        if (age == 1 && candles.length () > 1 && candles [candles.length () - 1] == '1')
            return -1;
 
        double lo = 0, hi = 1000;
 
        for (int it = 0; it < ITER; it++)
        {
            double mid = (lo + hi) / 2, p = 1, sum = 0;
 
            for (int i = (int) candles.length () - 1; i >= 0; i--)
            {
                if (candles [i] == '1')
                    sum += p;
 
                p *= mid;
            }
 
            if (sum < age)
                lo = mid;
            else
                hi = mid;
        }
        return lo;
	}
};
